package com.imoblife.brainwavestus;

import java.util.ArrayList;

import org.apache.http.util.LangUtils;

import com.imoblife.brainwavestus.adapter.ProductBuyAdapter;
import com.imoblife.brainwavestus.customview.ListViewItem;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.model.PurchaseProduct;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItem;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItem.DownLoadingListener;
import com.imoblife.brainwavestus.oldDownloadFrame.DownLoadItemPragrame;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.user.User;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;
import com.imoblife.brainwavestus.util.DBUtil;
import com.imoblife.brainwavestus.util.DebugUtil;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ProductDetailActivity extends BaseActivity {
	
	private ImageView mImgDetailIcon;
	private TextView mTvDetailTitle;
	private TextView mTvDetail;
	private Product mProduct;

	private ArrayList<Product> mDatas;
	private Store mStore;
	private PurchaseProduct purchaseProduct;
	@Override
	protected void onCreate(Bundle arg0) {
		
		super.onCreate(arg0);
		
		if (getBrainWavesApplication().isPad()) {
			
			setContentView(R.layout.tb_purchase_product);
		} else {
			
			setContentView(R.layout.product_show_layout);
		}
		
		initView();	

		ProductInfo info = mProduct.getProductInfo();
		String id = info.getId();
		
		purchaseProduct = 
				new PurchaseProduct(this,mStore,id);
		purchaseProduct.loading();
		purchaseProduct.setAction();
	}	
	
	private void initView() {
		
		initTitleName(getString(R.string.store));
		String productName = getIntent()
					.getStringExtra(Constant.PRODUCT_ID);
		mStore = getBrainWavesApplication().getStore();
		mImgDetailIcon = (ImageView)findViewById(R.id.icon);
		mTvDetailTitle = (TextView) findViewById(R.id.title);
		mTvDetail = (TextView) findViewById(R.id.detail);
		
		mImgDetailIcon.setImageBitmap(
					BitMapFlyweightFactory
					.getBitMapFlyweightFactory(getAssets())
					.getBitMapFlyweight(productName).getBitmap());
		
		mProduct = mStore.getProductByName(productName);
		ProductInfo info = mProduct.getProductInfo();
	
		mTvDetailTitle.setText(info.getDetailTitle());	
		mTvDetail.setText(info.getLongDescription());
		mDatas = new ArrayList<Product>();
		
		if ( info.isProductCollection()) {
			
			mDatas.addAll(mProduct.getAllChild());
			
			if(info.getDownloadState().equals(
					Constant.DOWNLOAD_STATE_NOT_ENBLE)) {
				
				mDatas.add(mProduct);
			}
		}else {
			
			mDatas.add(mProduct);
		}	
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		purchaseProduct.updataPurchaseList();
		purchaseProduct.registerDownloadListner();

	}
	
	@Override
	protected void onPause() {	
		super.onPause();
		purchaseProduct.unRegisterDownloadLister();
	}

	@Override
	protected void onDestroy() {
		purchaseProduct.release();
		super.onDestroy();
		System.gc();	
	}
	
	
	

}
